/*
    jQuery for handling URL hash changin by pressing browser back and forward button.
    Copyright (c) 2010 Keiji Hokamura (famteam)
    This plugin available for use in all personal or commercial projects under both MIT and GPL licenses.
 */

(function($) {
	$.hashMonitor = {
		handler: null,
		interval: 400,
		hash: null,
		timer: null,
		
		start: function(handler) {
			$.hashMonitor.handler = handler;
			$.hashMonitor.hash = window.location.hash;
			$.hashMonitor._check();
		},
		stop: function() {
			if ($.hashMonitor.timer != null) {
				clearTimeout($.hashMonitor.timer);
			}
		},
		_check: function() {
			if ($.hashMonitor.hash != null && $.hashMonitor.hash != window.location.hash) {
				var previousHash = $.hashMonitor.hash;
				$.hashMonitor.hash = window.location.hash;
				$.hashMonitor.handler(previousHash);
			}
			
			if ($.hashMonitor.handler != null) {
				$.hashMonitor.timer = setTimeout($.hashMonitor._check, $.hashMonitor.interval);
			}
		}
	};

})(jQuery);